import {Store, Getter, Mutation, Action} from 'decorators/vuex'
import {contains} from '@/utils/commonMethod/method.js'
@Store
export class ProductStore {
  allBrands = []
  topicProductArr = []

    // 存model集合
  productSelectFilter = {}

    // 存value集合
  productSelectValue = {}

  productCurrentSuperId = ''

  productCurrentSelects = []

  productBrandLetters = {}

  scrollY = 0

  @Getter
  topicProductArr(state) {
    return state.topicProductArr
  }

  @Getter
  scrollY(state) {
    return state.scrollY
  }

    @Getter
  productBrandLetters(state) {
    return state.productBrandLetters
  }

    @Getter
  allBrands(state) {
    return state.allBrands
  }

    @Getter
  productSelectFitler(state) {
    return state.productSelectFilter
  }

    @Getter
  productSelectValue(state) {
    return state.productSelectValue
  }

    @Getter
  productCurrentSelects(state) {
    return state.productCurrentSelects
  }

    @Getter
  productCurrentSuperId(state) {
    return state.productCurrentSuperId
  }

  @Mutation
  setScrollY(state, y) {
    state.scrollY = y
  }

  @Mutation
  setTopicProductArr(state, data) {
    state.topicProductArr = data
  }

    @Mutation
  setAllBrands(state, brands) {
    state.allBrands = brands
  }

    @Mutation
  setProductSelectValue(state, selectValue) {
    state.productSelectValue = selectValue
  }

    @Mutation
  setProductBrandLetters(state, letters) {
    state.productBrandLetters = letters
  }

    @Mutation
  setProductCurrentSelects(state, selectItems) {
    state.productCurrentSelects = selectItems
  }

    @Mutation
  setProductSelectFilter(state, selectFilter) {
    state.productSelectFilter = selectFilter
  }
  @Mutation
  clearTopicProductArr(state) {
    state.topicProductArr = []
  }

    @Mutation
  setProductCurrentSuperId(state, superId) {
    let arr = state.productSelectFilter[superId]
    if (!arr) {
      state.productSelectFilter[superId] = []
      state.productSelectValue[superId] = []
    }

    state.productCurrentSuperId = superId
  }

    @Action clearProductSelect({commit}) {
      commit('setProductSelectFilter', [])
      commit('setProductCurrentSuperId', '')
      commit('setProductSelectValue', {})
      commit('setProductCurrentSuperId', '')
    }

    @Action
  changeProductCurrentSuperId({commit}, superId) {
    commit('setProductCurrentSuperId', superId)
  }

    @Action selectProductFilterItem({state}, item) {
        // 通过superId 找到对应的已选中数组
      const superIdSelectArr = state.productSelectFilter[state.productCurrentSuperId]
      const superIdSelectValue = state.productSelectValue[state.productCurrentSuperId]

        // 判断选中的是否已存在
      if (contains(superIdSelectArr, item)) {
        for (let i in superIdSelectArr) {
          if (superIdSelectArr[i] === item) {
            superIdSelectArr.splice(i, 1)
            superIdSelectValue.splice(i, 1)
            state.productSelectFilter[state.productCurrentSuperId] = superIdSelectArr
            state.productSelectValue[state.productCurrentSuperId] = superIdSelectValue
            return
          }
        }
      } else {
        superIdSelectArr.push(item)
        if (item && item.value) {
          superIdSelectValue.push(item.value)
        }
        state.productSelectValue[state.productCurrentSuperId] = superIdSelectValue
        state.productSelectFilter[state.productCurrentSuperId] = superIdSelectArr
      }
    }

    @Action getProductSelectBySuperId({commit, state}, superId) {
      let arr = state.productSelectFilter[superId]
      commit('setProductCurrentSelects', arr)
    }
}
